package com.mano.mall.service.integral.impl;

import com.alibaba.fastjson.JSONObject;
import com.mano.mall.api.dto.integral.input.IntegralInputDTO;
import com.mano.mall.api.integral.IntegralService;
import com.mano.mall.common.base.BaseApiService;
import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.common.utils.MyBeanUtils;
import com.mano.mall.service.mapper.IntegralMapper;
import com.mano.mall.service.mapper.entity.IntegralEntity;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zj
 * @Description:
 * @Date: Created in 15:56 2020/10/30
 * @Modified By:
 */
@RestController
public class IntegralServiceImpl extends BaseApiService<JSONObject> implements IntegralService {

    @Autowired
    private IntegralMapper integralMapper;

    @Override
    @Transactional
    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> addIntegral(@Validated IntegralInputDTO integralInputDTO) {

        IntegralEntity integralEntity = MyBeanUtils.dtoToDo(integralInputDTO, IntegralEntity.class);
        integralMapper.insertIntegral(integralEntity);
        return setResultSuccess();
    }

    @Override
    public BaseResponse<JSONObject> removeIntegral(Integer id) {
        integralMapper.deleteIntegral(id);
        return setResultSuccess();
    }

    @Override
    public boolean checkPayIntegral(String paymentId) {
        IntegralEntity entity = integralMapper.findIntegral(paymentId);
        return null==entity?false:true;
    }
}
